var app = getApp({})
Page({
  data: {
    phoneState: false,
    passwordState: false,
    password: true,
    passwordEyeImgUrl: "/images/eyeClose.png",
    veri: "获取验证码",
    phone: "",
  },
  onLoad: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    //获取手机号
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/phone',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        that.setData({
          phone: res.data.message.phone
        })
      }
    });
  },
  //绑定手机
  //弹窗
  changePhoneState: function() {
    this.setData({
      phoneState: !this.data.phoneState
    })
  },
  //手机号提交
  submitPhone: function(e) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    if (e.detail.value.phoneText == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: "none",
        duration: 1500,
        mask: true
      })
    } else {
      wx.request({
        url: 'https://www.mamingwang.cn/api/openid/phone',
        data: {
          openid: openid,
          phone: e.detail.value.phoneText,
          code: e.detail.value.phoneVeri
        },
        method: "post",
        success(res) {
          console.log(res)
          if (res.data.code == -1) {
            wx.showToast({
              title: '验证码不正确',
              icon: "none",
              duration: 1500,
              mask: true
            })
          } else {
            that.setData({
              phoneState: !that.data.phoneState
            })
          }
        }
      })
    }
  },
  //验证码获取手机号
  getPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //获取验证码
  getVeri: function() {
    var openid = wx.getStorageSync("openid");
    var that = this;
    if (this.data.veri == "获取验证码" && this.data.phone) {
      that.setData({
        veri: 60
      })
      var flag = setInterval(function(veri) {
        that.setData({
          veri: that.data.veri - 1
        })
        if (that.data.veri == 0) {
          clearInterval(flag);
          that.data.veri = "获取验证码";
          that.setData({
            veri: that.data.veri
          })
        }
      }, 1000);
      wx.request({
        url: 'https://www.mamingwang.cn/api/openid/sendTemplateSMS',
        data: {
          openid: openid,
          phone: that.data.phone
        },
        method: "post",
        success(res) {
          console.log(res)
        }
      })
    } else if (this.data.veri == "获取验证码" && !this.data.phone) {
      wx.showToast({
        title: '请填写手机号',
        icon: "none",
        duration: 1500,
        mask: true
      })
    }
  },
  //设置密码
  //弹窗
  changePasswordState: function() {
    this.setData({
      passwordState: !this.data.passwordState
    })
  },
  //显示密码
  showPassWord: function() {
    var inputType = 'text';
    var passwordEyeImgUrl = '/images/eye.png';
    this.setData({
      password: false,
      passwordEyeImgUrl: passwordEyeImgUrl
    })
  },
  //取消显示密码
  cancelPassWord: function() {
    var inputType = 'password';
    var passwordEyeImgUrl = '/images/eyeClose.png';
    this.setData({
      password: true,
      passwordEyeImgUrl: passwordEyeImgUrl
    })
  },
  //提交密码
  submitPassWord: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    if (e.detail.value.newPassWord == "") {
      wx.showToast({
        title: '密码不能为空',
        icon: "none",
        duration: 2000,
        mask:true
      })
    } else {
      if (e.detail.value.newPassWord == e.detail.value.confirmPassWord) {
        if (e.detail.value.oldPassWord == e.detail.value.newPassWord) {
          wx.showToast({
            title: '新密码不能与旧密码一致',
            icon: "none",
            duration: 2000,
            mask:true
          })
        } else {
          //修改密码
          wx.request({
            url: 'https://www.mamingwang.cn/api/index/pwd',
            data: {
              openid: openid,
              pwd: e.detail.value.newPassWord,
              ypsd: e.detail.value.oldPassWord
            },
            method: "post",
            success(res) {
              if (res.data.code == -1) {
                wx.showToast({
                  title: '旧密码不正确',
                  icon: "none",
                  duration: 2000,
                  mask:true
                })
              } else {
                wx.showToast({
                  title: '修改成功',
                  icon: "none",
                  duration: 1500,
                  mask: true,
                  success() {
                    that.setData({
                      passwordState: !that.data.passwordState
                    })
                  }
                })
              }
            }
          })
        }
      } else {
        wx.showToast({
          title: '两次新密码不一致',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      }
    }
  },
})